package persistence.dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import persistence.db.DataSet;
import persistence.db.DatabaseConnector;
import persistence.model.AbstractModel;
import persistence.model.Seance;

public class SeanceDAO extends AbstractDAO{

	private Seance seance;
	
	public SeanceDAO() {
		setTableName("SEANCE");
	}
	
	public SeanceDAO(Seance se) {
		this();
		seance = se;
	}

	public DataSet all() {
		return new DataSet(this);
	}
	
	public AbstractModel makeModel(ResultSet rset) {
		try {
			setId(rset.getInt("id_seance"));
			return new Seance(this.getId(),
					rset.getInt("fk_seance_periode"),
					rset.getInt("fk_seance_salle"),
					rset.getInt("fk_seance_service")					
					);
		} catch (SQLException e) {
			System.out.println("makeModel error");
			e.printStackTrace();
		}
		return null;
	}

	public void save() {
		String query;
		ResultSet result;
		query = "INSERT INTO " + getTableName() + " VALUES ( S_ID_" + getTableName() + ".nextval, " +
			seance.getIdPeriode() + ", " +
			seance.getIdSalle() +", "+
			seance.getIdService() +")";
		/* Execute la requete */
		result = DatabaseConnector.getInstance().execute(query);
		setId(this.getId()); /* Mise a jour de l'identifiant */
	}

	public void save(int id) {
		String query;
		ResultSet result;
		query = "UPDATE " + getTableName() +
			" SET fk_seance_periode = " + seance.getIdPeriode() +
			", fk_seance_salle = " + seance.getIdSalle() +
			", fk_seance_service = "+ seance.getIdService() +
			" WHERE ID_"+ getTableName() + " = "+ id;
		/* Execute la requete */
		result = DatabaseConnector.getInstance().execute(query);
	}
}
